// pages/shop/shop.js
import S_request from '../../utils/requestService.js';
import CONFIG from '../../config.js';
import * as utils from '../../utils/util.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goldNum: 0,
    allShopList: null,
    myShopList: null,
    scrollHeight: 0,
    modalObj: {},
    isShow: false,
    isShowMobile: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      goldNum: utils.parseGoldNum(app.globalData.goldnum)
    });
    // this.getAllShop();
    // this.getGoldNum();
  },
  getAllShop: function() {
    let self = this;
    S_request.mine.getAllShop((result) => {
      let allShopList = result.lists;
      for (let i = 0; i < allShopList.length; i++) {
        let item = allShopList[i];
        item.gold = utils.parseGoldNum(item.money)+"金币"
        if (parseInt(item.money) <= app.globalData.goldnum) {
          item.btnStyle = 'background-color: #53d96c;';
        } else {
          item.btnStyle = 'background-color: #a9ecb5;';
        }
      }
      self.setData({
        allShopList: allShopList
      })
    })
  },
  shopRule: function() {
    this.data.modalObj.titS = '商城兑换规则'
    this.data.modalObj.contaStyle = 'height: 830rpx;'
    this.data.modalObj.text1 = '1.玩家进入商城，在金币足够的情况下，选择心仪的商品，点击兑换即可，兑换成功后则扣取玩家金币。'
    this.data.modalObj.text2 = '2.实物商品，将由客服人员每周二统计审核后，3~7个工作日发货，兑换的商品不提供发票。'
    this.data.modalObj.text3 = '3.话费卡券奖品，将由客服人员每周二统计审核后，1~3个工作日通过充值或则发送卡券码的形式告知兑换方。'
    // this.data.modalObj.textStyle = 'font-size: 24rpx;margin-top: 120rpx;text-align: left;'
    this.setData({
      modalDealIdx: 1,
      isShow: true,
      modalObj: this.data.modalObj
    })
  },
  modalLeftAc: function(e) {
    this.setData({
      modalDealIdx: 0,
      isShow: false,
      isShowMobile: false
    })
  },
  exchGoods: function(e) {
    let self = this;
    //{"errMsg":"chooseAddress:ok","userName":"张三","nationalCode":"510000","telNumber":"020-81167888","postalCode":"510000","provinceName":"广东省","cityName":"广州市","countyName":"海珠区","detailInfo":"新港中路397号"}
    let id = e.currentTarget.dataset.id;
    for (let i = 0; i < this.data.allShopList.length; i++) {
      let item = this.data.allShopList[i];
      if (item.id == id) {
        if (parseInt(item.money) <= app.globalData.goldnum) {
          //可以兑换 1表示不需要用户信息，2表示需要手机 3需要邮寄地址
          if (item.type == 2) {
            self.chooseMobile(id);
          } else if (item.type == 3) {
            self.chooseAddress(id);
          }
        } else {
          //不可兑换
          wx.showToast({
            title: '金币不足',
            icon: 'none',
            duration: 2000
          })
        }
        break;
      }
    }
  },
  chooseMobile: function(id) {
    this.data.modalObj.id = id;
    this.setData({
      modalDealIdx: 1,
      isShowMobile: true,
      modalObj: this.data.modalObj
    })
  },
  chooseAddress: function(id) {
    //自定义地址
    wx.navigateTo({
      url: '../shopExcAddr/shopExcAddr?goodId=' + id
    });
    //系统地址
    /*
    wx.chooseAddress({
      success: (res) => {
        let address = res.provinceName + " " + res.cityName + " " + res.countyName + " " + res.detailInfo;
        S_request.mine.submitShopInfo(id, res.telNumber, res.userName, address, (data) => {
          let orderId = data.order_id;
          console.log("orderId==" + orderId);
          wx.showToast({
            title: '兑换成功',
            icon: 'none',
            duration: 2000
          })
          this.getAllShop();
          this.getGoldNum();
        })

      }
    });
    */
  },
  //兑换成功
  modalRightAc: function(e) {
    let orderId = e.order_id;
    console.log("orderId==" + orderId);
    this.setData({
      modalDealIdx: 0,
      isShow: false,
      isShowMobile: false
    });
    wx.showToast({
      title: '兑换成功',
      icon: 'none',
      duration: 2000
    })
    this.getAllShop();
    this.getGoldNum();
  },
  getGoldNum: function(shareType) {
    S_request.index.getUserInfo((data) => {
      // console.log("fenxiang===="+JSON.stringify(data))
      app.globalData.goldnum = data.user.goldcoin;
      this.setData({
        goldNum: utils.parseGoldNum(app.globalData.goldnum)
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getAllShop();
    this.getGoldNum();

    // let allShopStyle = 'background-color: #fff;color: #2fbdfb;';
    // this.setData({
    //   allShopStyle: allShopStyle,
    //   bodyStyle: {
    //     allShop: 'display:block',
    //     myShop: 'display:none',
    //     noGood: 'display:none'
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function() {
        // console.log("ajhvahvh")
      }
    });
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    let obj = { 'pagepath': CONFIG.SHARE.pathShop }
    S_request.index.recordShare("-3", obj);
    return {
      title: CONFIG.SHARE.titleShop,
      path: CONFIG.SHARE.pathShop,
      imageUrl: CONFIG.SHARE.imagShopUrl
    }
  },
  excRecord: function() {
    wx.navigateTo({
      url: '../excRecord/excRecord'
    });
  }
})